package com.common.utils;

import java.math.BigInteger;

public class MaxPrefixUtil {

    /**
     * 将科目前缀编号 + 1
     * @param value
     * @return
     */
    public static String incrementValue(String value) {
        if (value == null || value.length() < 1 ) {
            return "";
        }

        String prefix = value.substring(0, 1); // "Z"
        String numberPart = value.substring(1); // "000051"

        if (numberPart.isEmpty()) {
            throw new IllegalArgumentException("无数字部分");
        }

        try {
            BigInteger num = new BigInteger(numberPart);
            num = num.add(BigInteger.ONE);
            String formatted = String.format("%0" + numberPart.length() + "d", num);
            // 检查长度是否一致，如果进位导致长度增加，可能需要处理
            if (formatted.length() != numberPart.length()) {
                formatted = String.format("%0" + numberPart.length() + "d", num.mod(BigInteger.TEN.pow(numberPart.length())));
            }
            return prefix + formatted;
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("数字部分包含非数字字符", e);
        }
    }
}
